
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.structure_prediction.substitutor &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.structure_prediction.substitutor</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.structure_prediction.substitutor</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides classes for predicting new structures from existing ones.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">itertools</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="n">mul</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MSONable</span>

<span class="kn">from</span> <span class="nn">pymatgen.core.periodic_table</span> <span class="kn">import</span> <span class="n">get_el_sp</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.structure_prediction.substitution_probability</span> \
    <span class="kn">import</span> <span class="nn">SubstitutionProbability</span>
<span class="kn">from</span> <span class="nn">pymatgen.transformations.standard_transformations</span> \
    <span class="kn">import</span> <span class="nn">SubstitutionTransformation</span>
<span class="kn">from</span> <span class="nn">pymatgen.alchemy.transmuters</span> <span class="kn">import</span> <span class="n">StandardTransmuter</span>
<span class="kn">from</span> <span class="nn">pymatgen.alchemy.materials</span> <span class="kn">import</span> <span class="n">TransformedStructure</span>
<span class="kn">from</span> <span class="nn">pymatgen.alchemy.filters</span> <span class="kn">import</span> <span class="n">RemoveDuplicatesFilter</span><span class="p">,</span> \
    <span class="n">RemoveExistingFilter</span>


<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Will Richards, Geoffroy Hautier&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2012, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.2&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Will Richards&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;wrichard@mit.edu&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;Aug 31, 2012&quot;</span>


<div class="viewcode-block" id="Substitutor"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor">[docs]</a><span class="k">class</span> <span class="nc">Substitutor</span><span class="p">(</span><span class="n">MSONable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This object uses a data mined ionic substitution approach to propose</span>
<span class="sd">    compounds likely to be stable. It relies on an algorithm presented in</span>
<span class="sd">    Hautier, G., Fischer, C., Ehrlacher, V., Jain, A., and Ceder, G. (2011).</span>
<span class="sd">    Data Mined Ionic Substitutions for the Discovery of New Compounds.</span>
<span class="sd">    Inorganic Chemistry, 50(2), 656-663. doi:10.1021/ic102031h</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">threshold</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This substitutor uses the substitution probability class to</span>
<span class="sd">        find good substitutions for a given chemistry or structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            threshold:</span>
<span class="sd">                probability threshold for predictions</span>
<span class="sd">            symprec:</span>
<span class="sd">                symmetry precision to determine if two structures</span>
<span class="sd">                are duplicates</span>
<span class="sd">            kwargs:</span>
<span class="sd">                kwargs for the SubstitutionProbability object</span>
<span class="sd">                lambda_table, alpha</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_kwargs</span> <span class="o">=</span> <span class="n">kwargs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span> <span class="o">=</span> <span class="n">SubstitutionProbability</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_threshold</span> <span class="o">=</span> <span class="n">threshold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_symprec</span> <span class="o">=</span> <span class="n">symprec</span>

<div class="viewcode-block" id="Substitutor.get_allowed_species"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor.get_allowed_species">[docs]</a>    <span class="k">def</span> <span class="nf">get_allowed_species</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the species in the domain of the probability function</span>
<span class="sd">        any other specie will not work</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">species</span></div>

<div class="viewcode-block" id="Substitutor.pred_from_structures"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor.pred_from_structures">[docs]</a>    <span class="k">def</span> <span class="nf">pred_from_structures</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target_species</span><span class="p">,</span> <span class="n">structures_list</span><span class="p">,</span>
                             <span class="n">remove_duplicates</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">remove_existing</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        performs a structure prediction targeting compounds containing all of</span>
<span class="sd">        the target_species, based on a list of structure (those structures</span>
<span class="sd">        can for instance come from a database like the ICSD). It will return</span>
<span class="sd">        all the structures formed by ionic substitutions with a probability</span>
<span class="sd">        higher than the threshold</span>

<span class="sd">        Notes:</span>
<span class="sd">        If the default probability model is used, input structures must</span>
<span class="sd">        be oxidation state decorated. See AutoOxiStateDecorationTransformation</span>

<span class="sd">        This method does not change the number of species in a structure. i.e</span>
<span class="sd">        if the number of target species is 3, only input structures containing</span>
<span class="sd">        3 species will be considered.</span>

<span class="sd">        Args:</span>
<span class="sd">            target_species:</span>
<span class="sd">                a list of species with oxidation states</span>
<span class="sd">                e.g., [Specie(&#39;Li&#39;,1),Specie(&#39;Ni&#39;,2), Specie(&#39;O&#39;,-2)]</span>

<span class="sd">            structures_list:</span>
<span class="sd">                a list of dictionnary of the form {&#39;structure&#39;:Structure object</span>
<span class="sd">                ,&#39;id&#39;:some id where it comes from}</span>
<span class="sd">                the id can for instance refer to an ICSD id.</span>

<span class="sd">            remove_duplicates:</span>
<span class="sd">                if True, the duplicates in the predicted structures will</span>
<span class="sd">                be removed</span>

<span class="sd">            remove_existing:</span>
<span class="sd">                if True, the predicted structures that already exist in the</span>
<span class="sd">                structures_list will be removed</span>

<span class="sd">        Returns:</span>
<span class="sd">            a list of TransformedStructure objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">target_species</span> <span class="o">=</span> <span class="n">get_el_sp</span><span class="p">(</span><span class="n">target_species</span><span class="p">)</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">transmuter</span> <span class="o">=</span> <span class="n">StandardTransmuter</span><span class="p">([])</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">target_species</span><span class="p">)</span> <span class="o">&amp;</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_allowed_species</span><span class="p">())))</span> \
                <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">target_species</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;the species in target_species are not allowed &quot;</span>
                             <span class="o">+</span> <span class="s2">&quot;for the probability model you are using&quot;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">permut</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(</span><span class="n">target_species</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">structures_list</span><span class="p">:</span>
                <span class="c1"># check if: species are in the domain,</span>
                <span class="c1"># and the probability of subst. is above the threshold</span>
                <span class="n">els</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="s1">&#39;structure&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">permut</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">els</span><span class="p">)</span> <span class="o">&amp;</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_allowed_species</span><span class="p">())))</span> <span class="o">==</span> \
                        <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">cond_prob_list</span><span class="p">(</span><span class="n">permut</span><span class="p">,</span> <span class="n">els</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_threshold</span><span class="p">:</span>

                    <span class="n">clean_subst</span> <span class="o">=</span> <span class="p">{</span><span class="n">els</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="n">permut</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                                   <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">))</span>
                                   <span class="k">if</span> <span class="n">els</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">permut</span><span class="p">[</span><span class="n">i</span><span class="p">]}</span>

                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">clean_subst</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="k">continue</span>

                    <span class="n">transf</span> <span class="o">=</span> <span class="n">SubstitutionTransformation</span><span class="p">(</span><span class="n">clean_subst</span><span class="p">)</span>

                    <span class="k">if</span> <span class="n">Substitutor</span><span class="o">.</span><span class="n">_is_charge_balanced</span><span class="p">(</span>
                            <span class="n">transf</span><span class="o">.</span><span class="n">apply_transformation</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="s1">&#39;structure&#39;</span><span class="p">])):</span>
                        <span class="n">ts</span> <span class="o">=</span> <span class="n">TransformedStructure</span><span class="p">(</span>
                            <span class="n">s</span><span class="p">[</span><span class="s1">&#39;structure&#39;</span><span class="p">],</span> <span class="p">[</span><span class="n">transf</span><span class="p">],</span>
                            <span class="n">history</span><span class="o">=</span><span class="p">[{</span><span class="s2">&quot;source&quot;</span><span class="p">:</span> <span class="n">s</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]}],</span>
                            <span class="n">other_parameters</span><span class="o">=</span><span class="p">{</span>
                                <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;structure_prediction&#39;</span><span class="p">,</span>
                                <span class="s1">&#39;proba&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">cond_prob_list</span><span class="p">(</span><span class="n">permut</span><span class="p">,</span> <span class="n">els</span><span class="p">)}</span>
                        <span class="p">)</span>
                        <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ts</span><span class="p">)</span>
                        <span class="n">transmuter</span><span class="o">.</span><span class="n">append_transformed_structures</span><span class="p">([</span><span class="n">ts</span><span class="p">])</span>

        <span class="k">if</span> <span class="n">remove_duplicates</span><span class="p">:</span>
            <span class="n">transmuter</span><span class="o">.</span><span class="n">apply_filter</span><span class="p">(</span><span class="n">RemoveDuplicatesFilter</span><span class="p">(</span>
                <span class="n">symprec</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_symprec</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">remove_existing</span><span class="p">:</span>
            <span class="c1"># Make the list of structures from structures_list that corresponds to the</span>
            <span class="c1"># target species</span>
            <span class="n">chemsys</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="n">sp</span><span class="o">.</span><span class="n">symbol</span> <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="n">target_species</span><span class="p">]))</span>
            <span class="n">structures_list_target</span> <span class="o">=</span> <span class="p">[</span><span class="n">st</span><span class="p">[</span><span class="s1">&#39;structure&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">st</span> <span class="ow">in</span> <span class="n">structures_list</span>
                                      <span class="k">if</span> <span class="n">Substitutor</span><span class="o">.</span><span class="n">_is_from_chemical_system</span><span class="p">(</span>
                    <span class="n">chemsys</span><span class="p">,</span>
                    <span class="n">st</span><span class="p">[</span><span class="s1">&#39;structure&#39;</span><span class="p">])]</span>
            <span class="n">transmuter</span><span class="o">.</span><span class="n">apply_filter</span><span class="p">(</span><span class="n">RemoveExistingFilter</span><span class="p">(</span><span class="n">structures_list_target</span><span class="p">,</span>
                                                         <span class="n">symprec</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_symprec</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">transmuter</span><span class="o">.</span><span class="n">transformed_structures</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_is_charge_balanced</span><span class="p">(</span><span class="n">struct</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        checks if the structure object is charge balanced</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">sum</span><span class="p">([</span><span class="n">s</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">struct</span><span class="o">.</span><span class="n">sites</span><span class="p">])</span> <span class="o">==</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_is_from_chemical_system</span><span class="p">(</span><span class="n">chemical_system</span><span class="p">,</span> <span class="n">struct</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        checks if the structure object is from the given chemical system</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">chemsys</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="n">sp</span><span class="o">.</span><span class="n">symbol</span> <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="n">struct</span><span class="o">.</span><span class="n">composition</span><span class="p">]))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">chemsys</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">chemical_system</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">chemsys</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">el</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">chemical_system</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="Substitutor.pred_from_list"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor.pred_from_list">[docs]</a>    <span class="k">def</span> <span class="nf">pred_from_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">species_list</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        There are an exceptionally large number of substitutions to</span>
<span class="sd">        look at (260^n), where n is the number of species in the</span>
<span class="sd">        list. We need a more efficient than brute force way of going</span>
<span class="sd">        through these possibilities. The brute force method would be::</span>

<span class="sd">            output = []</span>
<span class="sd">            for p in itertools.product(self._sp.species_list</span>
<span class="sd">                                       , repeat = len(species_list)):</span>
<span class="sd">                if self._sp.conditional_probability_list(p, species_list)</span>
<span class="sd">                                       &gt; self._threshold:</span>
<span class="sd">                    output.append(dict(zip(species_list,p)))</span>
<span class="sd">            return output</span>

<span class="sd">        Instead of that we do a branch and bound.</span>

<span class="sd">        Args:</span>
<span class="sd">            species_list:</span>
<span class="sd">                list of species in the starting structure</span>

<span class="sd">        Returns:</span>
<span class="sd">            list of dictionaries, each including a substitutions</span>
<span class="sd">            dictionary, and a probability value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">species_list</span> <span class="o">=</span> <span class="n">get_el_sp</span><span class="p">(</span><span class="n">species_list</span><span class="p">)</span>
        <span class="c1"># calculate the highest probabilities to help us stop the recursion</span>
        <span class="n">max_probabilities</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">s2</span> <span class="ow">in</span> <span class="n">species_list</span><span class="p">:</span>
            <span class="n">max_p</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">for</span> <span class="n">s1</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">species</span><span class="p">:</span>
                <span class="n">max_p</span> <span class="o">=</span> <span class="nb">max</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">cond_prob</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">),</span> <span class="n">max_p</span><span class="p">])</span>
            <span class="n">max_probabilities</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">max_p</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">def</span> <span class="nf">_recurse</span><span class="p">(</span><span class="n">output_prob</span><span class="p">,</span> <span class="n">output_species</span><span class="p">):</span>
            <span class="n">best_case_prob</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">max_probabilities</span><span class="p">)</span>
            <span class="n">best_case_prob</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">output_prob</span><span class="p">)]</span> <span class="o">=</span> <span class="n">output_prob</span>
            <span class="k">if</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">mul</span><span class="p">,</span> <span class="n">best_case_prob</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_threshold</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">output_species</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">species_list</span><span class="p">):</span>
                    <span class="n">odict</span> <span class="o">=</span> <span class="p">{</span>
                        <span class="s1">&#39;substitutions&#39;</span><span class="p">:</span>
                            <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">species_list</span><span class="p">,</span> <span class="n">output_species</span><span class="p">)),</span>
                        <span class="s1">&#39;probability&#39;</span><span class="p">:</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">mul</span><span class="p">,</span> <span class="n">best_case_prob</span><span class="p">)}</span>
                    <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">odict</span><span class="p">)</span>
                    <span class="k">return</span>
                <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">species</span><span class="p">:</span>
                    <span class="n">i</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">output_prob</span><span class="p">)</span>
                    <span class="n">prob</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sp</span><span class="o">.</span><span class="n">cond_prob</span><span class="p">(</span><span class="n">sp</span><span class="p">,</span> <span class="n">species_list</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                    <span class="n">_recurse</span><span class="p">(</span><span class="n">output_prob</span> <span class="o">+</span> <span class="p">[</span><span class="n">prob</span><span class="p">],</span> <span class="n">output_species</span> <span class="o">+</span> <span class="p">[</span><span class="n">sp</span><span class="p">])</span>

        <span class="n">_recurse</span><span class="p">([],</span> <span class="p">[])</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> substitutions found&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">output</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">output</span></div>

<div class="viewcode-block" id="Substitutor.pred_from_comp"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor.pred_from_comp">[docs]</a>    <span class="k">def</span> <span class="nf">pred_from_comp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">composition</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Similar to pred_from_list except this method returns a list after</span>
<span class="sd">        checking that compositions are charge balanced.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">predictions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pred_from_list</span><span class="p">(</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">predictions</span><span class="p">:</span>
            <span class="n">subs</span> <span class="o">=</span> <span class="n">p</span><span class="p">[</span><span class="s1">&#39;substitutions&#39;</span><span class="p">]</span>
            <span class="n">charge</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">for</span> <span class="n">i_el</span> <span class="ow">in</span> <span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
                <span class="n">f_el</span> <span class="o">=</span> <span class="n">subs</span><span class="p">[</span><span class="n">i_el</span><span class="p">]</span>
                <span class="n">charge</span> <span class="o">+=</span> <span class="n">f_el</span><span class="o">.</span><span class="n">oxi_state</span> <span class="o">*</span> <span class="n">composition</span><span class="p">[</span><span class="n">i_el</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">charge</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> charge balanced &#39;</span>
                     <span class="s1">&#39;compositions found&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">output</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">output</span></div>

<div class="viewcode-block" id="Substitutor.as_dict"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">__version__</span><span class="p">,</span>
                <span class="s2">&quot;kwargs&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kwargs</span><span class="p">,</span> <span class="s2">&quot;threshold&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_threshold</span><span class="p">,</span>
                <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">}</span></div>

<div class="viewcode-block" id="Substitutor.from_dict"><a class="viewcode-back" href="../../../../pymatgen.analysis.structure_prediction.substitutor.html#pymatgen.analysis.structure_prediction.substitutor.Substitutor.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            d (dict): Dict representation</span>

<span class="sd">        Returns:</span>
<span class="sd">            Class</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;threshold&#39;</span><span class="p">]</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;kwargs&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">threshold</span><span class="o">=</span><span class="n">t</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.structure_prediction.substitutor</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>